package com.example.smssender;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

/**
 * 发送进度列表适配器
 */
public class SendingProgressAdapter extends RecyclerView.Adapter<SendingProgressAdapter.ViewHolder> {
    
    private List<SendingStatus> statusList;
    
    public SendingProgressAdapter(List<SendingStatus> statusList) {
        this.statusList = statusList != null ? new ArrayList<>(statusList) : new ArrayList<>();
    }
    
    public void updateStatusList(List<SendingStatus> newStatusList) {
        this.statusList = newStatusList != null ? new ArrayList<>(newStatusList) : new ArrayList<>();
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_sending_progress, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SendingStatus status = statusList.get(position);
        holder.bind(status);
    }
    
    @Override
    public int getItemCount() {
        return statusList.size();
    }
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvContactName;
        private TextView tvPhoneNumber;
        private TextView tvStatus;
        
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvContactName = itemView.findViewById(R.id.tvContactName);
            tvPhoneNumber = itemView.findViewById(R.id.tvPhoneNumber);
            tvStatus = itemView.findViewById(R.id.tvStatus);
        }
        
        void bind(SendingStatus status) {
            if (status == null) {
                return;
            }
            
            // 显示联系人名称或手机号
            String displayName = status.getDisplayName();
            tvContactName.setText(displayName);
            
            // 显示手机号（如果联系人名称存在，则显示手机号；否则显示"未知"）
            String phoneNumber = status.getPhoneNumber();
            if (phoneNumber != null && !phoneNumber.isEmpty()) {
                tvPhoneNumber.setText(phoneNumber);
            } else {
                tvPhoneNumber.setText("未知");
            }
            
            // 显示状态
            String statusText = status.getStatusText();
            if (status.getStatus() == SendingStatus.STATUS_FAILED && status.getErrorMsg() != null) {
                statusText += ": " + status.getErrorMsg();
            }
            tvStatus.setText(statusText);
            
            // 根据状态设置文本颜色
            int color;
            switch (status.getStatus()) {
                case SendingStatus.STATUS_SUCCESS:
                    color = 0xFF4CAF50; // 绿色
                    break;
                case SendingStatus.STATUS_FAILED:
                    color = 0xFFF44336; // 红色
                    break;
                case SendingStatus.STATUS_SENDING:
                    color = 0xFFFF9800; // 橙色
                    break;
                default:
                    color = 0xFF757575; // 灰色
                    break;
            }
            tvStatus.setTextColor(color);
        }
    }
}

